package searchAlgs;

import Graph.Edge;
import Graph.Graph;
import Graph.Vertex;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Ruan,Tiago
 */
public class AlgDFS {
    
    private Map<Integer,String> color = new HashMap();
    private Graph graph;
    private Map<Integer,Integer> d = new HashMap();
    private Map<Integer,Integer> f = new HashMap();
    private Map<Integer,Vertex> q = new HashMap();
    private Map<Integer,Vertex>precursor;
    private int time;
    
    public AlgDFS(Graph g){
        graph = g;
        precursor = new HashMap();
        for(Map.Entry<Integer,Vertex> entry: graph.getVertexes().entrySet()){
            Integer integer = entry.getKey();
            Vertex vertex = entry.getValue();
            color.put(vertex.getId(), "WHITE");
        }
        
    }
    
    public void doDFS(Graph g){
        for(int i = 1;i<=g.getVertexes().size();i++){
            // d.put(g.getVertexes().get(i).getId(),0);
            //color.put(g.getVertexes().get(i).getId(),"GRAY");
            //u = g.getVertexes().get(i);
            if(color.get(g.getVertexes().get(i).getId()).equals("WHITE")){
                doDFSVisit(g.getVertexes().get(i));
            }
        }
        
    }
    
    public void doDFSVisit(Vertex u){
        color.remove(u.getId());
        color.put(u.getId(), "GRAY");
        time = time+1;
        d.put(u.getId(), time);
        for(Edge v : u.getEdges()){
            if(color.get(v.getVj().getId()).equals("WHITE")){
                precursor.put(v.getVj().getId(), u);
                doDFSVisit(v.getVj());
            }
        }
        color.remove(u.getId());
        color.put(u.getId(),"BLACK");
        time = time+1;
        f.put(u.getId(), time);
        System.out.println(u.getId()+"("+d.get(u.getId())+","+f.get(u.getId())+")");
    }
    
}